package com.hqu.environment.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.hqu.environment.annotation.LoginUser;
import com.hqu.environment.constant.Constant;
import com.hqu.environment.dao.EquipmentCategoryDao;
import com.hqu.environment.dao.EquipmentDao;
import com.hqu.environment.dto.AuthorityDTO;
import com.hqu.environment.dto.EquipmentCategoryDTO;
import com.hqu.environment.dto.EquipmentDTO;
import com.hqu.environment.dto.FarmCategoryDTO;
import com.hqu.environment.dto.FarmDTO;
import com.hqu.environment.entity.EquipmentCategory;
import com.hqu.environment.utils.QRCodeUtil;
import com.hqu.environment.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @Description: 设备controller
 * @Date: 2019/04/23 13:16
 * @Author: 邓勇嘉
 **/
@Controller
@RequestMapping("admin/equipment")
public class AdminEquipmentController {

    @Autowired
    EquipmentDao equipmentDao;

    @Autowired
    EquipmentCategoryDao equipmentCategoryDao;


    @RequestMapping("/getEquipmentPageInfo")
    @ResponseBody
    public Result getEquipmentPageInfo() {
        List<EquipmentCategoryDTO> ec=equipmentCategoryDao.selectList(null);
        return Result.success().add("equipmentCategoryList",ec);
    }

    @RequestMapping("/getList")
    @ResponseBody
    public Result getEquipmentList(EquipmentDTO equipmentDTO) {
        List<EquipmentDTO> equipmentList = equipmentDao.selectList(equipmentDTO);
        return Result.success().add("equipmentList",equipmentList);
    }

    @RequestMapping("/getById")
    @ResponseBody
    public Result getEquipmentById(Integer id) {
        EquipmentDTO equipmentDTO = equipmentDao.selectByPrimaryKey(id);
        return Result.success().add("equipment",equipmentDTO);
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        equipmentDao.deleteByPrimaryKey(id);
        return Result.success();
    }


    @RequestMapping("/add")
    @ResponseBody
    public Result addEquipment(@LoginUser Integer userId, @RequestBody EquipmentDTO equipmentDTO) throws Exception {
        equipmentDTO.setCreator(userId);
        equipmentDTO.setCreateTime(new Date());
        equipmentDao.insertSelective(equipmentDTO);


        //String s="{\"id\":\""+equipmentDTO.getId()+"\",\"type\":\"equipment\"}";
        //String codeUrl= QRCodeUtil.encode(s, Constant.LOGO,Constant.DEST,true);
        //EquipmentDTO equipmentDTOwithCodeUrl=new EquipmentDTO();
        //equipmentDTOwithCodeUrl.setId(equipmentDTO.getId());
        //equipmentDTOwithCodeUrl.setCodeUrl(codeUrl);
        //equipmentDao.updateByPrimaryKeySelective(equipmentDTOwithCodeUrl);
        return Result.success();
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result updateEquipment(@LoginUser Integer userId, @RequestBody EquipmentDTO equipmentDTO) {
        equipmentDTO.setCreator(userId);
        equipmentDTO.setCreateTime(new Date());
        equipmentDao.updateByPrimaryKeySelective(equipmentDTO);
        return Result.success();
    }

    @RequestMapping("/deleteList")
    @ResponseBody
    public Result deleteEquipmentList(String ids) {
        List<Integer> list = JSONObject.parseArray(ids, Integer.class);
        equipmentDao.delete(list);
        return Result.success();
    }

}
